package core

import (
  "errors"
  Account "gitee.com/yizhisamoye/GoWeChat/account"
  MiniProgram "gitee.com/yizhisamoye/GoWeChat/miniProgram"
  "strings"
)

type Container struct {
  AccountObj     map[string]*Account.WxAccount
  MiniProgramObj map[string]*MiniProgram.WxMiniProgram
}
const(
  ACCOUNT=iota
  MINIPROGRAM
)

func init(){

}

func (c *Container)GetAccount(name string) *Account.WxAccount {
  if v,exist:=c.AccountObj[name];exist{
    return v
  }
  return nil
}
func(c *Container)SetAccount(name string,obj *Account.WxAccount){
  if obj!=nil{
    c.AccountObj[name]=obj
  }
}
func (c *Container)GetMiniProgram(name string) *MiniProgram.WxMiniProgram {
  if v,exist:=c.MiniProgramObj[name];exist{
    return v
  }
  return nil
}
func(c *Container)SetMiniProgram(name string,obj *MiniProgram.WxMiniProgram ){
  if obj!=nil{
    c.MiniProgramObj[name]=obj
  }
}
//生成微信实体
func (c *Container)CreateWxEntity(entityType int,appId, appSecret string,alias string,options ...map[string]string) (WxEntityInterface, error) {
  if entityType==ACCOUNT{
    acEntity,err:=Account.NewWxAccount(appId,appSecret,options...)
    if err!=nil{
     return nil,err
    }
    if strings.TrimSpace(alias)==""{
      c.SetAccount(appId,acEntity)
    }else{
      c.SetAccount(alias,acEntity)
    }
    return acEntity,err
  }else if entityType==MINIPROGRAM{
    mpEntity,err:=MiniProgram.NewWxMiniProgram(appId,appSecret,options...)
    if err!=nil{
      return nil,err
    }
    if strings.TrimSpace(alias)==""{
      c.SetMiniProgram(appId,mpEntity)
    }else{
      c.SetMiniProgram(alias,mpEntity)
    }
    return mpEntity,err
  }
  return nil,errors.New("微信实体类型不存在");
}

